
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GLES/gl.h> /* For NDK*/
#include <assert.h>
#include <android/log.h>

#include "app.h"
#include "utils.h"
#include "jade.h"


// Total run length is 20 * camera track base unit length (see cams.h).
#define RUN_LENGTH  (20 * CAMTRACK_LEN)


static long sStartTick = 0;
static long sTick = 0;

static int  action_idx = 0;

static jade_world_t *world = NULL;
static jade_camera_t *camera = NULL;
static jade_model_md2_t *model = NULL;
static jade_plane_t *plane = NULL;
static jade_skydome_t *skydome = NULL;
static float angle = 0.0f;
static jade_font_t *font = NULL;

///////////////////////////////////////////////////////////////////////////////

// Called from the app framework.
void appInit(int width, int height)
{
    jade_texture_t *texture = NULL; 

    world = (jade_world_t*)jade_world_create (width, height, (GLfloat)255.0f, (GLfloat)255.0f, (GLfloat)255.0f, (GLfloat)0.0f);

    camera = (jade_camera_t*)jade_camera_create ();
    jade_camera_set_eye (camera, 0.0f, 10.0f, 30.0f);
    //jade_camera_set_eye (camera, 0.0f, 10.0f, 30.0f);

    jade_world_set_camera (world, camera); 

    model = jade_model_md2_create ("/data/mii/mii_centered.md2");
    if (model != NULL) {

        texture = (jade_texture_t*)jade_texture_create ("/data/mii/mii_hair_nose_mouth_256_256.bmp"); 

        if (texture != NULL) {
            jade_model_md2_set_texture_id (model, texture->textureId);
            jade_texture_destroy (texture);
        }

        //jade_model_md2_set_scale (model, (double)15.0f, (double)15.0f, (double)15.0f);

        jade_model_md2_set_action_index (model, action_idx);        
    }

    font = (jade_font_t*)jade_font_create (16, 16, 12, 18, "/data/mii/font.bmp");
    if (font == NULL)
        LOGD ("font = NULL.\n");


    plane = jade_plane_create (4, 4, 12.0f);

    if (plane != NULL) {
        
        texture = (jade_texture_t*)jade_texture_create ("/data/mii/floor.bmp"); 
        if (texture != NULL) {
            jade_plane_set_texture_id (plane, texture->textureId);
            jade_texture_destroy (texture);
        }

        jade_plane_set_position (plane, -10.0f, -2.0f, -3.0f);
    }

  

    skydome = jade_skydome_create (256, 30.0f, 10.0f);

    if (skydome != NULL) {
        
        texture = (jade_texture_t*)jade_texture_create ("/data/mii/clouds.bmp"); 
        if (texture != NULL) {
            jade_skydome_set_texture_id (skydome, texture->textureId);
            jade_texture_destroy (texture);
        }

        jade_skydome_set_position (skydome, 5.0f, -120.0f, -280.0f);
        //jade_skydome_set_position (skydome, 5.0f, -70.0f, -280.0f);
        //jade_skydome_set_position (skydome, 5.0f, -20.0f, -280.0f);
        //jade_skydome_set_position (skydome, 0.0f, (float)(-256 * sinf(DTOR * 10.0f)) - 28.0f, 0.0f);
    }



}


// Called from the app framework.
void appDeinit()
{
    if (model != NULL) 
        jade_model_md2_destroy (model);

    if (font != NULL)
        jade_font_destroy (font);

    if (plane != NULL)
        jade_plane_destroy (plane);

    if (skydome != NULL)
        jade_skydome_destroy (skydome);

    if (camera != NULL)
        jade_camera_destroy (camera);

    if (world != NULL)
        jade_world_destroy (world);
}

// Called from the app framework.
/* The tick is current time in milliseconds, width and height
 * are the image dimensions to be rendered.
 */
void appRender(long tick, int width, int height)
{
    if (sStartTick == 0)
        sStartTick = tick;

    // Actual tick value is "blurred" a little bit.
    sTick = (sTick + tick - sStartTick) >> 1;

    // Prepare OpenGL ES for rendering of the frame.
    jade_world_prepare_render(world);

/*
    gluLookAt(0.0f, 0.0f, 30.0f,
	          0.0f, 0.0f, 0.0f,
	          0.0f, 1.0f, 0.0f);
*/

    jade_skydome_set_rotate (skydome, -90.0f, 0.0f, angle);
    jade_skydome_render (skydome);

    jade_plane_render (plane);

    jade_model_md2_prepare_frame (model);
    jade_model_md2_render (model);

    angle += 0.1f;

}

void appSetModelTextureOffset (float x, float y, float z) {


    LOGD ("appSetModelTextureOffset: x=%f, y=%f, z=%f\n", x, y, z);

    jade_model_md2_set_texture_offset (model, (GLfloat)x, (GLfloat)y, (GLfloat)z);

}


void appSetModelRotate (float x, float y, float z) {

    jade_model_md2_set_rotate (model, (GLfloat)x, (GLfloat)y, (GLfloat)z);

}

void appSetModelScale (float x, float y, float z) {

    jade_model_md2_set_scale (model, (GLfloat)x, (GLfloat)y, (GLfloat)z);

}

void appSetModelPosition (float x, float y, float z) {

    jade_model_md2_set_position (model, (GLfloat)x, (GLfloat)y, (GLfloat)z);

}

void fontRender (int x, int y, const char *string) {

    jade_font_draw_string (font, x, y, string);
}
